package logic

import (
	"anban/service"
	"anban/utils"
	"anban/utils/wechat"
	"fmt"
	"net/url"
)

// 老师新增班级
func AddClass(input url.Values) *utils.Error {
	if len(input["user_id"][0]) <= 0 {
		return utils.NewError(0, "请提供老师")
	}
	if len(input["class_id"][0]) <= 0 {
		return utils.NewError(0, "请选择班级")
	}
	if len(input["course_id"][0]) <= 0 {
		return utils.NewError(0, "请选择科目")
	}

	p := map[string]interface{}{}
	p["userId"] = input["user_id"][0]
	p["classId"] = input["class_id"][0]
	userClass := service.GetUserClassInfo(p)
	if userClass.Id > 0 {
		return utils.NewError(0, "班级添加失败，您已经添加过这个班级了")
	}

	_, err := service.AddUserClass(input)
	if err != nil {
		return utils.NewError(0, "班级添加失败")
	}

	return nil
}

// 提现
func Withdraw(input url.Values) *utils.Error {
	var p map[string]interface{}

	if len(input["user_id"][0]) <= 0 {
		return utils.NewError(0, "请提供会员")
	}
	if len(input["amount"][0]) <= 0 {
		return utils.NewError(0, "请输入提现金额")
	}
	amount := utils.Atof64(input["amount"][0])
	if amount < 1 {
		return utils.NewError(0, "每次提现至少1元")
	}

	p = map[string]interface{}{}
	p["id"] = input["user_id"][0]
	user := service.GetUserInfo(p)
	if user.Status == 0 {
		return utils.NewError(0, "您的账号已被禁用")
	}
	if len(user.OpenId) <= 0 {
		return utils.NewError(0, "您的openid异常，无法提现")
	}
	if amount > user.WithdrawMoney {
		return utils.NewError(0, "您的零钱余额不足")
	}

	withdrawRecordId, err := service.AddWithdrawRecord(input)
	if err != nil {
		return utils.NewError(0, fmt.Sprintf("提现失败%s", err.Error()))
	}

	var editInput url.Values
	var addInput url.Values

	withdrawMoney := user.WithdrawMoney - amount
	editInput = url.Values{
		"id": {fmt.Sprintf("%d", user.Id)},
		"withdraw_money": {fmt.Sprintf("%f", withdrawMoney)},
	}
	_, err = service.EditUser(editInput)
	if err != nil {
		return utils.NewError(0, fmt.Sprintf("提现失败%s", err.Error()))
	}

	p = map[string]interface{}{}
	p["id"] = withdrawRecordId
	withdrawRecord := service.GetWithdrawRecordInfo(p)
	enterprisePayment := wechat.NewEnterprisePayment(int(amount*100), user.OpenId, withdrawRecord.Sn, "提现收入")
	enterprisePaymentRes, err := enterprisePayment.Pay()
	if err != nil {
		editInput = url.Values{
			"id": {fmt.Sprintf("%d",withdrawRecord.Id)},
			"status": {"2"},
			"fail_msg": {err.Error()},
		}
		service.EditWithdrawRecord(editInput)
		return utils.NewError(0, fmt.Sprintf("提现失败%s", err.Error()))
	}
	if enterprisePaymentRes.ReturnCode == "FAIL" || enterprisePaymentRes.ResultCode == "FAIL" {
		editInput = url.Values{
			"id": {fmt.Sprintf("%d",withdrawRecord.Id)},
			"status": {"2"},
			"fail_msg": {enterprisePaymentRes.ReturnMsg},
		}
		service.EditWithdrawRecord(editInput)
		return utils.NewError(0, fmt.Sprintf("提现失败%s", enterprisePaymentRes.ReturnMsg))
	}

	editInput = url.Values{
		"id": {fmt.Sprintf("%d", withdrawRecord.Id)},
		"status": {"1"},
	}
	service.EditWithdrawRecord(editInput)

	addInput = url.Values{
		"user_id": {fmt.Sprintf("%d", user.Id)},
		"type": {"2"},
		"amount": {fmt.Sprintf("%f", withdrawRecord.Amount)},
	}
	service.AddFinance(addInput)

	return nil
}